#include "radiomicsalgorithm.h"
#include "segmentalgorithm.h"
#include "extractalgorithm.h"
#include "predictalgorithm.h"
#include "UiSegmentation.h"

RadiomicsAlgorithm::RadiomicsAlgorithm(QObject *parent):
    m_param(new AlgoParam),
    m_predParam(new PredictParam),
    algo(new AbstractAlgorithm),
    QObject(parent)
{
    m_param->pMask = 0;
    m_param->iStart = -1;
    m_param->iCount = 0;
}

RadiomicsAlgorithm::~RadiomicsAlgorithm()
{
    clearParam();
    delete m_param;
    delete m_predParam;
    delete algo;
}

void RadiomicsAlgorithm::setSeriesVolume(const ushort **pVolume, ulong width, ulong height, ulong slice)
{
    clearParam();
    m_param->pVolume = pVolume;
    m_param->iWidth = width;
    m_param->iHeight = height;
    m_param->iSlice = slice;
    if (slice > 0) m_param->pMask = new uchar*[slice];
    for (ulong i = 0; i < slice; ++i) {
        ulong size = width*height*sizeof(uchar);
        m_param->pMask[i] = new uchar[size];
        memset(m_param->pMask[i], 0, size);
    }
}

void RadiomicsAlgorithm::setSeedInfo(int x, int y, int z, int val, int area)
{
    m_param->seed.x = x;
    m_param->seed.y = y;
    m_param->seed.z = z;
    //m_param->seed.v = val;
    m_param->seed.r = area;
    if (m_param->pVolume && m_param->pVolume[z])
        m_param->seed.v = m_param->pVolume[z][y*m_param->iWidth+x];
}

void RadiomicsAlgorithm::getTumorMask(const uchar **&pMask, int &start, int &count) const
{
    pMask = (const uchar**)m_param->pMask;
    start = m_param->iStart;
    count = m_param->iCount;
}

void RadiomicsAlgorithm::clearParam()
{
    if (m_param->pMask) {
        for (int i = 0; i < m_param->iSlice; ++i)
            delete[] m_param->pMask[i];
        delete[] m_param->pMask;
        m_param->pMask = 0;
    }
}

void RadiomicsAlgorithm::terminate()
{
    algo->terminate();
}

void RadiomicsAlgorithm::doSegment()
{
    delete algo;
    switch (m_combo.segment) {
    case AS_Default:
    default:
        algo = new SegmentAlgorithm(m_param);
        break;
    }
    connect(algo, SIGNAL(resultReady(bool,QString)), this, SIGNAL(segmentFinished(bool,QString)));
    //connect(algo, SIGNAL(finished()), algo, SLOT(deleteLater()));
    algo->start();
}

void RadiomicsAlgorithm::doExtract()
{
    delete algo;
    m_param->OutPutData = PROGNOSTIC_FEATURE_FILE;
    switch (m_combo.extract) {
    case AE_Default:
    default:
        algo = new ExtractAlgorithm(m_param);
        break;
    }
    connect(algo, SIGNAL(resultReady(bool,QString)), this, SIGNAL(extractFinished(bool,QString)));
    //connect(algo, SIGNAL(finished()), algo, SLOT(deleteLater()));
    algo->start();
}

void RadiomicsAlgorithm::doPredict()
{
    delete algo;
    switch (m_combo.predict) {
    case AP_Default:
    default:
        algo = new PredictAlgorithm(m_predParam);
        break;
    }
    connect(algo, SIGNAL(resultReady(bool,QString)), this, SIGNAL(predictFinished(bool,QString)));
    //connect(algo, SIGNAL(finished()), algo, SLOT(deleteLater()));
    algo->start();
}
